package vg.modules.search;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.event_and_request.event.UIEventOpenNewGraph;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AMenuItem;

/**
 * This class for search of vertexes. 
 * @author tzolotuhin
 */
public class SearchPlugin implements IPlugin {
	private PluginParameter parameter; // plugin parameter
	private JMenuItem jMenuItem;
	private SearchPanel searchPanel;
	/**
	 * Installer.
	 */
	public void install(final PluginParameter param) throws Exception {
		this.parameter = param;
		//---------------------------------------
		this.searchPanel = new SearchPanel("Search panel", param);
		this.jMenuItem = new JMenuItem("Search");
		this.jMenuItem.setIcon(new ImageIcon("./data/resources/textures/search.png"));
		this.jMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchPanel.open();
			}
		});
		this.jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));
		this.parameter.userInterface.addMenuItem(new AMenuItem(this.jMenuItem) {
			public void update(Observable o, Object arg) {
				if(arg instanceof AUIEvent) {
					final AUIEvent event = (AUIEvent)arg;
					switch (event.getType()) {
						case DEF_CLOSE_PROGRAM: {
							searchPanel.dispose();
							break;
						}
						case DEF_OPEN_NEW_GRAPH: {
							UIEventOpenNewGraph buf = (UIEventOpenNewGraph)event;
							searchPanel.addGraph(buf.getGraphId(), buf.getGraphName());
							break;
						}
						case DEF_CHANGE_UI_STYLE: {
							searchPanel.updateUITheme();
							break;
						}
						case DEF_CHANGE_TAB: {
							UIEventChangeTab bufEvent = (UIEventChangeTab)event;
							ATab tab = bufEvent.getTab();
							if (tab != null) {
								switch (tab.getTabType()) {
									case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
										searchPanel.setCurrentView((AGraphView)tab);
										break;
									}							
								}
							}
							break;
						}
						case DEF_RESET: {
							searchPanel.reset();
							break;
						}
						case DEF_OPEN_NEW_TAB: {
							
							break;
						}
						case DEF_CLOSE_TAB: {
							
							break;
						}
					}
				}
			}
		}, "edit");
		this.searchPanel.updateUITheme();
	}
}
